package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.ResultEntity;

import com.net.tkc.pro.entity.TakeTwo;
import com.net.tkc.pro.impl.TakeTwoImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/TakeTwo/")
public class TakeTwoController {
    ResultEntity resultEntity = null;
    @Resource
    TakeTwoImpl takeTwo;
    /**
     * 根据id删除评论
     * @param id 评论id
     * @return
     */
    @DeleteMapping("Take")
    private String delTakeTwo(@RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            int result = takeTwo.delTakeTwo(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 查看帖子
     * @param takeid 帖子id
     * @return
     */
    @GetMapping("GetTake")
    private String getTakeList(
            @RequestParam("takeid") int takeid
    ){
        resultEntity = new ResultEntity();
        try{
            List<TakeTwo> list=takeTwo.getTakeList(takeid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 更改状态
     * @param examine 状态
     * @param id id
     * @return
     */
    @PutMapping("Take")
    private String upExamine(
            @RequestParam("examine") int examine,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = takeTwo.upExamine(examine,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("取消关注成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("取消关注失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 发布评论
     * @param takeid 帖子id
     * @param userid 发布评论的人
     * @param reply 评论内容
     * @return
     */
    @PostMapping("Take")
    private String addTakeTwo(
            @RequestParam("takeid") int takeid,
            @RequestParam("userid") int userid,
            @RequestParam("reply") String reply
    ){
        resultEntity = new ResultEntity();
        TakeTwo take=new TakeTwo();
        take.setTakeid(takeid);
        take.setUserid(userid);
        take.setReply(reply);
        try{
            int result = takeTwo.addTakeTwo(take);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
